package cc.kzc.adminauth.config;

import java.time.Duration;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ObjectUtils;

@Configuration
public class Redis1Config {

	/**
	 * pool配置
	 */
	@Value("${spring.redis1.timeout:30000}")
	private long timeout;

	@Value("${spring.redis1.lettuce.pool.max-active:8}")
	private int maxActive;

	@Value("${spring.redis1.lettuce.pool.max-wait:-1}")
	private long maxWait;

	@Value("${spring.redis1.lettuce.pool.max-idle:8}")
	private int maxIdle;

	@Value("${spring.redis1.lettuce.pool.min-idle:0}")
	private int minIdle;

	// redis1 配置
	@Value("${spring.redis1.host}")
	private String redis_host;

	@Value("${spring.redis1.port}")
	private int redis_port;

	@Value("${spring.redis1.password}")
	private String redis_password;

	@Value("${spring.redis1.database:1}")
	private int redis_db;

	@Bean(name = "redis1ConnectionFactory")
	public RedisConnectionFactory redisConnectionFactory() {
		// 基本配置
		RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
		configuration.setHostName(redis_host);
		configuration.setPort(redis_port);
		configuration.setDatabase(redis_db);
		if (!ObjectUtils.isEmpty(redis_password)) {
			RedisPassword redisPassword = RedisPassword.of(redis_password);
			configuration.setPassword(redisPassword);
		}

		// 连接池通用配置
		GenericObjectPoolConfig<?> genericObjectPoolConfig = new GenericObjectPoolConfig<>();
		genericObjectPoolConfig.setMaxTotal(maxActive);
		genericObjectPoolConfig.setMaxWait(Duration.ofMillis(maxWait));
		genericObjectPoolConfig.setMaxIdle(maxIdle);
		genericObjectPoolConfig.setMinIdle(minIdle);

		// Lettuce Pool
		LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration
				.builder();
		builder.poolConfig(genericObjectPoolConfig);
		builder.commandTimeout(Duration.ofMillis(timeout));
		LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, builder.build());
		connectionFactory.afterPropertiesSet();
		return connectionFactory;
	}

	@Bean(name = "redis1Template")
	public StringRedisTemplate redisTemplate(@Qualifier("redis1ConnectionFactory") RedisConnectionFactory factory) {

		// redisTemplate
		StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(factory);
		return stringRedisTemplate;
	}

}
